<?php
//header ("Content-Type:text/xml");
require 'functions.php';

$string = '';
$string  .= "<xml>";

$rows = array();
//echo $string;
$resultado1 = sqlQueryToDB("select * from cliente");

while($row =sqlFetchArray($resultado1, "ASSOC")){
  $rows[] = $row;
}


foreach($rows as $row)
{
    $string .= "<cliente>";
    $string .= "<razon_social>";
    $string .= $row['razon_social'];
    $string .= "</razon_social>";
    $cliente = $row['cliente_id'];


    $resultado2 = sqlQueryToDB("SELECT * FROM maquina AS MQ INNER JOIN maquinacliente as MC ON MQ.maquina_id=MC.maquina_id where MC.cliente_id=$cliente");


    while ($row2=sqlFetchArray($resultado2, "ASSOC"))
    {
        $string .= "<maquina>";
        $string .= "<nombre>";
        $string .= $row2['nombre'];
        $string .= "</nombre>";
        $string .= "<marca>";
        $string .= $row2['marca'];
        $string .= "</marca>";
        $string .= "<cantidad>";
        $string .= $row2['contador'];
        $string .= "</cantidad>";
        $string .= "</maquina>";
    }

   $resultado2 = sqlQueryToDB("SELECT * FROM vendedor AS VE natural join cliente as CL where cliente_id=$cliente");
    while ($row2=sqlFetchArray($resultado2, "ASSOC"))
    {
        $string .= "<vendedor>";
        $string .= "<nombre-vendedor>";
        $string .= $row2['nombre'];
        $string .= "</nombre-vendedor>";
        $string .= "<apellido-vendedor>";
        $string .= $row2['apellido'];
        $string .= "</apellido-vendedor>";
        $string .= "</vendedor>";
    }

    $resultado2 = sqlQueryToDB("SELECT * FROM zona AS ZO NATURAL JOIN cliente as CL where cliente_id=$cliente");
    while ($row2=sqlFetchArray($resultado2, "ASSOC"))
    {
        $string .= "<zona>";
        $string .= "<nombre-zona>";
        $string .= $row2['nombre'];
        $string .= "</nombre-zona>";
        $string .= "</zona>";
    }

    $resultado2 = sqlQueryToDB("SELECT * FROM cliente as CL natural join contacto as CO natural join telefono as TE where cliente_id=$cliente");
    while ($row2=sqlFetchArray($resultado2, "ASSOC"))
    {
        $string .= "<contacto>";
        $string .= "<nombre-contacto>";
        $string .= $row2['nombre'];
        $string .= "</nombre-contacto>";
        $string .= "<apellido-contacto>";
        $string .= $row2['apellido'];
        $string .= "</apellido-contacto>";
        $string .= "<numero-telefono>";
        $string .= $row2['numero_telefono'];
        $string .= "</numero-telefono>";
        $string .= "</contacto>";
    }

    $string .= "</cliente>";
}

$string .= "</xml>";
$xml = new SimpleXMLElement($string);

$clientes = $xml->xpath('/xml/cliente');

foreach ($clientes as $cliente)
{
    $cantidades = $cliente->xpath('./maquina/cantidad');
    $total_maquinas = 0;
    foreach($cantidades as $cantidad)
    {
        $total_maquinas += (int) $cantidad;
    }

    if ($total_maquinas > 2)
    {
        $asd = $cliente->xpath("./razon_social");
        echo  $asd[0]."<br> \n";
    }
}
//print_r($xml);
?>
